#include "testview.h"
#include "ui_testview.h"
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QMessageBox>
TestView::TestView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestView),
    s_(0)
{
    ui->setupUi(this);
}

TestView::~TestView()
{
    delete ui;
}

void TestView::mousePressEvent(QMouseEvent *event)
{
    if(s_ == 0 || s_ == 3)
    {
        p_[0] = Point(event->x(),event->y());
        s_ = 1;
        this->repaint();
        return;
    }

    if(s_ == 1)
    {
        p_[1] = Point(event->x(),event->y());
        s_ = 2;
        this->repaint();
        return;
    }

    if(s_ == 2)
    {
        p_[2] = Point(event->x(),event->y());
        Point::PointSiteEnum site = Point::PointSite(p_[2],p_[0],p_[1]);
        QString msg = "";
        switch(site)
        {
        case Point::kPointAtLeft:
            msg = "left";
            break;
        case Point::kPointAtRight:
            msg = "right";
            break;
        case Point::kPointOnLine:
            msg = "on line";
            break;
        default:
            msg = "error";
        }
        QMessageBox::information(this,"a",msg);

        s_ = 3;
        this->repaint();
        return;
    }
}

void TestView::paintEvent(QPaintEvent *e)
{
    if(s_ == 0)
        return;

    QPainter painter(this);
    painter.setPen(Qt::red);
    painter.drawEllipse(p_[0].x(),p_[0].y(),2,2);

    if(s_ == 1)
        return;

    painter.setPen(Qt::green);
    painter.drawEllipse(p_[1].x(),p_[1].y(),2,2);
    painter.setPen(Qt::red);
    painter.drawLine(p_[0].x(),p_[0].y(),p_[1].x(),p_[1].y());
    if(s_ == 2)
        return;

    painter.setPen(Qt::blue);
        painter.drawEllipse(p_[2].x(),p_[2].y(),2,2);
}
